package val.to.UI;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class SelectedLocationOverlay extends ItemizedOverlay {
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	Context mContext;
	Drawable mMarker;
	Boolean mChoosingMode;

	public SelectedLocationOverlay(Drawable defaultMarker, Context context,
			Boolean ChoosingMode) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
		mMarker = defaultMarker;
		mOverlays = new ArrayList<OverlayItem>();
		mChoosingMode = ChoosingMode;
		populate();
	}

	public SelectedLocationOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		mOverlays = new ArrayList<OverlayItem>();
		populate();
		mMarker = defaultMarker;
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	public void addOverlay(OverlayItem overlay) {
		mOverlays.clear();
		mOverlays.add(overlay);
		populate();
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	// @Override
	// protected boolean onTap(int index) {
	// OverlayItem item = mOverlays.get(index);
	// AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
	// dialog.setTitle(item.getTitle());
	// dialog.setMessage(item.getSnippet());
	// dialog.show();
	// return true;
	// }

	int state = 0;

	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		if (super.onTouchEvent(event, mapView))
			return true;
		if(!mChoosingMode)
			return false;
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			state = 0;
		} else if (event.getAction() == MotionEvent.ACTION_UP && state == 0)
			state = 1;
		else
			state = 2;
		// ---when user lifts his finger---
		if (state == 1) {
			GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),
					(int) event.getY());

			if (!IsTapCloseToItem(
					new Point((int) event.getX(), (int) event.getY()), mapView)) {
				OverlayItem item = new OverlayItem(p, "tap to select", "test");
				addOverlay(item);
				Toast.makeText(mContext,
						"Tap on the icon to select the location",
						Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(mContext, "Location selected",
						Toast.LENGTH_SHORT).show();

				((Activity) mContext).getIntent().putExtra("Lat",
						p.getLatitudeE6());
				((Activity) mContext).getIntent().putExtra("Lon",
						p.getLongitudeE6());
				((Activity) mContext).setResult(Activity.RESULT_OK,
						((Activity) mContext).getIntent());
				((Activity) mContext).finish();
			}
			return true;
		}
		return false;
	}

	private boolean IsTapCloseToItem(Point tapPoint, MapView mv) {
		if (size() == 0)
			return false;
		Rect rec = mMarker.getBounds();

		Point overlayPoint = new Point();
		mv.getProjection().toPixels(mOverlays.get(0).getPoint(), overlayPoint);
		int xDiff = Math.abs(overlayPoint.x - tapPoint.x);
		int yDiff = overlayPoint.y - tapPoint.y;

		if (xDiff < rec.width() / 2 && yDiff < rec.height() && yDiff >= 0)
			return true;
		return false;

	}

	// @Override
	// public boolean onTap(final GeoPoint p, MapView view)
	// {
	// OverlayItem item = new OverlayItem(p, "tap to select", "test");
	// addOverlay(item);
	// return true;
	// }
}
